package com.novax.ex.activity.provider.controller.inner;

import com.novax.ex.activity.infrastructure.entity.OperationV2Config;
import com.novax.ex.activity.infrastructure.entity.OperationV2SubConfig;
import com.novax.ex.activity.open.api.inner.OperationV2SubConfigApi;
import com.novax.ex.activity.open.model.query.OperationV2SubConfigQuery;
import com.novax.ex.activity.open.model.request.OperationV2SubConfigRequest;
import com.novax.ex.activity.open.model.response.OperationV2SubConfigResponse;
import com.novax.ex.activity.provider.controller.BaseActivityController;
import com.novax.ex.activity.provider.service.OperationV2ConfigService;
import com.novax.ex.activity.provider.service.OperationV2SubConfigService;
import com.novax.ex.activity.provider.service.OperationV2SubRuleService;
import com.novax.ex.common.base.BaseEntity;
import com.novax.ex.common.constant.InternationalCode;
import com.novax.ex.common.enums.LanguageType;
import com.novax.ex.common.enums.OperationStatus;
import com.novax.ex.common.results.ReturnResult;
import com.novax.ex.common.util.CopyUtils;
import com.novax.ex.common.util.PageUtil;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @Description 活动落地页配置V2 子配置
 * @Author ChenXi
 * @Date 2023/04/18/10:08
 */

@RestController
public class OperationV2SubConfigController extends BaseActivityController implements OperationV2SubConfigApi {

    @Resource
    private OperationV2SubConfigService operationV2SubConfigService;
    @Resource
    private OperationV2ConfigService operationV2ConfigService;
    @Resource
    private OperationV2SubRuleService operationV2SubRuleService;

    @Override
    public ReturnResult<PageUtil<OperationV2SubConfigResponse>> page(OperationV2SubConfigQuery query) {
        PageUtil<OperationV2SubConfigResponse> page = new PageUtil<>(query);
        page.setCondition(Collections.singletonMap("query", query));
        operationV2SubConfigService.findPage(page, OperationV2SubConfigResponse.class);
        return ReturnResult.success(page);
    }

    @Override
    public ReturnResult<?> add(String language, OperationV2SubConfigRequest operationV2SubConfigRequest,
                               BindingResult bindingResult) {
        // 缺少必填项
        if(bindingResult.hasErrors()){
            return ReturnResult.fail(getInternational(language, InternationalCode.LACK_VALUE));
        }
        OperationV2SubConfig operationV2SubConfig = CopyUtils.copyObject(operationV2SubConfigRequest, OperationV2SubConfig.class);
        operationV2SubConfig.setState(OperationStatus.close.getType());
        boolean res = operationV2SubConfigService.addSelective(operationV2SubConfig);
        return res ? ReturnResult.success(getInternational(language, InternationalCode.OPERATION_SUCCESS))
            : ReturnResult.fail(getInternational(language, InternationalCode.OPERATION_ERROR));
    }

    @Override
    public ReturnResult<?> modify(String language, Long operationSubId, OperationV2SubConfigRequest operationV2SubConfigRequest,
                                  BindingResult bindingResult) {
        // 缺少必填项
        if(bindingResult.hasErrors()){
            return ReturnResult.fail(getInternational(language, InternationalCode.LACK_VALUE));
        }
        OperationV2SubConfig operationV2SubConfig = CopyUtils.copyObject(operationV2SubConfigRequest, OperationV2SubConfig.class);
        operationV2SubConfig.setId(operationSubId);
        operationV2SubConfig.setOperationId(null);

        // 如果是开启活动则必须得有配置至少一个子活动且有英文活动标题与规则
        if(operationV2SubConfigRequest.getState().equals(OperationStatus.open.getType())){
            // 主活动 多语言配置
            if(operationV2SubRuleService.queryOperationIdAndLanguage(operationSubId, LanguageType.en.getType()) <= 0){
                return ReturnResult.fail("需要多语言配置至少配置英文");
            }
        }

        // 若主活动开启时, 只有最后一个子活动开启中, 则不可关闭(主活动开启, 至少要有一个子活动保持开启中)
        // 关闭子活动请求
        if(operationV2SubConfigRequest.getState().equals(OperationStatus.close.getType())){
            OperationV2SubConfig current = operationV2SubConfigService.find(operationSubId);
            // 子活动开启中
            if(Objects.nonNull(current) && current.getState().equals(OperationStatus.open.getType())){
                OperationV2Config operationV2Config = operationV2ConfigService.find(current.getOperationId());
                // 主活动开启中
                if(Objects.nonNull(operationV2Config) && operationV2Config.getState().equals(OperationStatus.open.getType())){
                    // 主活动只有唯一这一个开启中的子活动
                    List<OperationV2SubConfig> list = operationV2SubConfigService.queryOpenList(current.getOperationId());
                    if(list.size() == 1 && list.get(0).getId().equals(operationSubId)){
                        return ReturnResult.fail("主活动开启时, 至少要有一个子活动开启中, 且不可关闭, 不可删除");
                    }
                }
            }
        }


        boolean res = operationV2SubConfigService.modifySelective(operationV2SubConfig);
        return res ? ReturnResult.success(getInternational(language, InternationalCode.OPERATION_SUCCESS))
            : ReturnResult.fail(getInternational(language, InternationalCode.OPERATION_ERROR));
    }

    @Override
    public ReturnResult<?> remove(String language, Long operationSubId) {
        OperationV2SubConfig operationV2SubConfig = operationV2SubConfigService.find(operationSubId);
        if(Objects.isNull(operationV2SubConfig)){
            return ReturnResult.fail(getInternational(language, InternationalCode.NOT_FOUND));
        }
        if (OperationStatus.open.getType().equals(operationV2SubConfig.getState())){
            return ReturnResult.fail(getInternational(language, InternationalCode.ACTIVITY_STATUS));
        }
        boolean res = operationV2SubConfigService.remove(operationSubId);
        return res ? ReturnResult.success(getInternational(language, InternationalCode.OPERATION_SUCCESS))
            : ReturnResult.fail(getInternational(language, InternationalCode.OPERATION_ERROR));
    }

    @Override
    public ReturnResult<?> batchRemove(String language, String ids) {
        boolean res = operationV2SubConfigService.batchRemove(ids);
        return res ? ReturnResult.success(getInternational(language, InternationalCode.OPERATION_SUCCESS))
            : ReturnResult.fail(getInternational(language, InternationalCode.OPERATION_ERROR));
    }

}
